define(["jquery", "easy-admin", "echarts", "echarts-theme", "miniAdmin", "miniTab"], function ($, ea, echarts, undefined, miniAdmin, miniTab) {

    var Controller = {
        index: function () {
            var options = {
                iniUrl: ea.url('ajax/initAdmin'),    // 初始化接口
                clearUrl: ea.url("ajax/clearCache"), // 缓存清理接口
                urlHashLocation: true,      // 是否打开hash定位
                bgColorDefault: false,      // 主题默认配置
                multiModule: true,          // 是否开启多模块
                menuChildOpen: false,       // 是否默认展开菜单
                loadingTime: 0,             // 初始化加载时间
                pageAnim: true,             // iframe窗口动画
                maxTabNum: 20,              // 最大的tab打开数量
            };
            miniAdmin.render(options);

            $('.login-out').on("click", function () {
                ea.request.get({
                    url: 'login/out',
                    prefix: true,
                }, function (res) {
                    ea.msg.success(res.msg, function () {
                        window.location = ea.url('login/index');
                    })
                });
            });
        },
        welcome: function () {

            get_contrast_data(start_time+' - '+end_time,1)
            get_class_data(class_time)
            get_class_count()
            get_class_year(year)

            $('#select_type').change(function(){
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                get_contrast_data(start_time+' - '+end_time,$(this).val())
            })

            $('#select_city').change(function(){
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                var type = $('#select_type option:selected').val();
                get_contrast_data(start_time+' - '+end_time,type,$(this).val())
            })

            $('#select_class_city').change(function(){
                var class_time = $('#class_time').val();
                get_class_data(class_time,$(this).val())
            })

            $('#select_class_year').change(function(){
                var class_time = $('#class_year').val();
                get_class_year(class_time,$(this).val())
            })

            var laydate = layui.laydate;
            laydate.render({
                elem: '#class_year'
                ,type: 'year'
                ,trigger: 'click'
                ,value: year
                ,done:function(value){
                    var type = $('#select_class_year option:selected').val();
                    get_class_year(value,type)
                }
            });
            laydate.render({
                elem: '#class_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: class_time
                ,done:function(value){
                    get_class_data(value)
                }
            });
            laydate.render({
                elem: '#start_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: start_time
                ,done:function(value){
                    var type = $('#select_type option:selected').val();
                    var end_time = $('#end_time').val();
                    get_contrast_data(value+' - '+end_time,type);
                }
            });
            laydate.render({
                elem: '#end_time'
                ,type: 'month'
                ,trigger: 'click'
                ,range: true
                ,value: end_time
                ,done:function(value){
                    var type = $('#select_type option:selected').val();
                    var start_time = $('#start_time').val();
                    get_contrast_data(start_time+' - '+value,type);
                }
            });

            miniTab.listen();

        },
        editAdmin: function () {
            ea.listen();
        },
        editPassword: function () {
            ea.listen();
        },
    };

    /**
     * 年度分类api
     */
    function get_class_year(val,type=0){
        ea.request.get({
            url: 'index/getClassYear?time='+val+'&type='+type,
            prefix: true,
        }, function (res) {
        },function (res){
            if(res.code == 200){
                echarts_year(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }

    /**
     * 环比费用报表api
     */
    function get_contrast_data(val='',type=1,city=0){

        ea.request.get({
            url: 'index/getContrastData?time='+val+'&type='+type+'&city='+city,
            prefix: true,
        }, function (res) {

        },function (res){
            if(res.code == 200){

                echarts_cross(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }

    /**
     * 月度费用报表api
     */
    function get_class_data(val,city=0){
        ea.request.get({
            url: 'index/getClassData?time='+val+'&city='+city,
            prefix: true,
        }, function (res) {
        },function (res){
            if(res.code == 200){
                echarts_class(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }

    /**
     * 饼图api
     */
    function get_class_count(){
        ea.request.get({
            url: 'index/getClassCount',
            prefix: true,
        }, function (res) {
        },function (res){
            if(res.code == 200){
                class_count(res.data)
            }else{
                ea.msg.error(res.msg)
            }
        },'',false);
    }


    /**
     * 年度分类报表
     */
    function echarts_year(data){
        var type = $('#select_class_year option:selected').text();
        if(type == '全部'){
            type = '所有分类';
        }
        var class_time = $('#class_year').val();
        var echartsRecords_class_count = echarts.init(document.getElementById('echarts-year'), 'walden');
        if(data.length == 0){
            var class_year_option = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var class_year_option = {
                title: {
                    text:class_time+'年'+type+'每月统计',
                    // x:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.name
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        smooth: true,
                        data: data.val,
                        type: 'bar',
                        label:{
                            show:true,
                            position:'top',
                        }
                    }
                ]
            };
        }
        echartsRecords_class_count.setOption(class_year_option);
        window.addEventListener("resize", function () {
            echartsRecords_class_count.resize();
        });
    }


    /**
     * 总占比报表
     */
    function class_count(data){
        var echartsRecords_class_count = echarts.init(document.getElementById('class_count'), 'walden');
        if(data.length == 0){
            var class_option = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                }
            }
        }else{
            var class_option = {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b} : {c} ({d}%)'
                },
                series: [
                    {
                        type: 'pie',
                        radius: '80%',
                        data: data,
                        label:{
                            normal:{
                                show:true,
                                formatter: '{b}({d}%)'
                            }
                        }
                    }
                ]
            };
        }


        echartsRecords_class_count.setOption(class_option);
        window.addEventListener("resize", function () {
            echartsRecords_class_count.resize();
        });
    }


    /**
     * 环比费用报表
     */
    function echarts_cross(data){
        var echartsRecords = echarts.init(document.getElementById('echarts-cross'), 'walden');
        var colors = ['#5470C6', '#EE6666'];
        if(data.length == 0){
            var option = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['分类','分类','分类','分类']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var series_data = [];
            for(var i=0;i<data.series_arr.length;i++){
                if(i == 0){
                    var s = {
                        name: data.series_arr[i].name,
                        type: 'line',
                        xAxisIndex: 1,
                        smooth: true,
                        emphasis: {
                            focus: 'series'
                        },
                        data: data.series_arr[i].data,
                        label:{
                            show:true,
                        }
                    }
                }else{
                    var s = {
                        name: data.series_arr[i].name,
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series'
                        },
                        data: data.series_arr[i].data,
                        label:{
                            show:true,
                        }
                    }
                }
                series_data.push(s)
            }
            var option = {
                color: colors,
                tooltip: {
                    trigger: 'none',
                    axisPointer: {
                        type: 'cross'
                    }
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                legend: {
                    data:data.time
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {
                            alignWithLabel: true
                        },
                        axisLine: {
                            onZero: false,
                            lineStyle: {
                                color: colors[1]
                            }
                        },
                        axisPointer: {
                            label: {
                                formatter: function (params) {
                                    return params.value
                                        + (params.seriesData.length ? '：' + params.seriesData[0].data : '');
                                }
                            }
                        },
                        data: data.name,
                        axisLabel: { interval: 0, rotate: 30 }
                    },
                    {
                        type: 'category',
                        axisTick: {
                            alignWithLabel: true
                        },
                        axisLine: {
                            onZero: false,
                            lineStyle: {
                                color: colors[0]
                            }
                        },
                        axisPointer: {
                            label: {
                                formatter: function (params) {
                                    return params.value
                                        + (params.seriesData.length ? '：' + params.seriesData[0].data : '');
                                }
                            }
                        },
                        data: data.name,
                        axisLabel: { interval: 0, rotate: 30 }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: series_data
            };
        }

        echartsRecords.setOption(option);
        window.addEventListener("resize", function () {
            echartsRecords.resize();
        });
    }


    /**
     * 分类总费用报表
     */
    function echarts_class(data){
        var echartsRecords = echarts.init(document.getElementById('echarts-records'), 'walden');
        if(data.length==0){
            var optionRecords = {
                title : {
                    text:'暂无数据',
                    x:'center',
                    y:'center',
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['分类','分类','分类','分类']
                },
                yAxis: {
                    type: 'value',
                    min: 100,
                    max: 200
                },
            }
        }else{
            var series = [];
            for (var key in data.series) {
                var s = {
                    name: key,
                    type: 'line',
                    data: data.series[key],
                    label:{
                        show:true,
                    }
                }
                series.push(s)
            }

            var optionRecords = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: data.keys
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.name,
                    axisLabel: { interval: 0, rotate: 30 }
                },
                yAxis: {
                    type: 'value'
                },
                series: series
            };
        }

        echartsRecords.setOption(optionRecords);
        window.addEventListener("resize", function () {
            echartsRecords.resize();
        });
    }

    return Controller;
});
